/**
 * 
 */
$(window.getTopicContent = function() {
	$.ajax({
		type : "post",
		url : "GetTopicAndReplyServlet",
		data : {
			topic_id : topic_id,
			pageNow : pageNow,
			pageCount : pageCount
		},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			var contentNode, pgtNode, topiclistNode, editNode;
			contentNode = $("#ct");
			// 页面工具
			pgtNode = setPagesTools(jsondata.pagenum, jsondata.pagenow);
			contentNode.html(pgtNode);
			// 楼层
			topiclistNode = setTopicContent(jsondata);
			contentNode.append(topiclistNode);
			// 页面工具
			pgtNode = setPagesTools(jsondata.pagenum, jsondata.pagenow);
			contentNode.append(pgtNode);
			// 编译框
			editNode = setReplyEdite();
			contentNode.append(editNode);
			contentNode.find("#editor").xheditor({
		        plugins: getCodePlugin(),
		        tools: "Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Img,Emot,Table,Code,|,Source,Fullscreen",
		        upImgUrl: "!../learning/uploadimg.jsp",
		        loadCSS: "../css/editor.css"
		    });
		},
		error : function() {
			// TODO do something
		}
	});
});
// topic content
function setTopicContent(topicContent) {
	var postListNode, topicTitleNode, topicTopNode, topicNode;
	postListNode = $("<div></div>").addClass("pl bm").attr("id", "postlist");
	topicTitleNode = $("<table cellspacing=\"0\" cellpadding=\"0\"></table>");
	topicTitleNode
			.append("<tbody><tr><td class=\"pls ptn pbn\"><div class=\"hm ptn\"><span class=\"xg1\">查看:</span><span class=\"xi1\">"
					+ topicContent.topic.Hit
					+ "</span>"
					+ "<span class=\"pipe\">|</span><span class=\"xg1\">回复:</span><span class=\"xi1\">"
					+ topicContent.topic.ReplyCount
					+ "</span></div></td><td class=\"plc ptm pbn vwthd\">"
					+ "<h1 class=\"ts\"><span id=\"thread_subject\">"
					+ topicContent.topic.Title
					+ "</span></h1>"
					+ "</span></td></tr></tbody>");
	postListNode.append(topicTitleNode);
	postListNode
			.append("<table class=\"ad\" cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"pls\"></td><td class=\"plc\"></td></tr></tbody></table>");
	// load 顶楼
	topicTopNode = setTopicTop(topicContent.topic);
	postListNode.append(topicTopNode);
	// load 楼层
	$.each(topicContent.reply, function(n, value) {
		topicNode = setreplaylist(n, value);
		postListNode.append(topicNode);
	});

	return postListNode;
}
// load 顶楼
function setTopicTop(topictop) {
	var newTopicNode, newTableNode, newPerTDNode, newConTDNode, newContrTDNode;
	// TODO 顶楼节点ID
	newTopicNode = $("<div></div>").attr("id", "top_" + topictop.ID);
	newTableNode = $(
			"<table cellspacing=\"0\" cellpadding=\"0\" summary=\"pid5581684\"></table>")
			.addClass("plhin");
	newTableNode.append("<tbody></tbody>");
	newTableNode.children().append("<tr id=\"maintr\"></tr>");
	newTableNode.children().append("<tr></tr>");
	newTableNode.children().append(
			"<tr id=\"contr\"><td class=\"pls\"></td></tr>");
	newTableNode
			.children()
			.append(
					"<tr class=\"ad\"><td class=\"pls\"> </td><td class=\"plc\"> </td></tr>");
	// 用户个人信息 TODO ；用户个人信息链接 头像 帖子数量 积分 昵称 等级
	newPerTDNode = $("<td class=\"pls\" rowspan=\"2\"><div id=\"favatar5728534\" class=\"pls favatar\" style=\"top: 33px;\"><div class=\"pi\">"
			+ "<div class=\"authi\"><a class=\"xw1\" target=\"_blank\" href=\"\">"
			+ topictop.userNickName
			+ "</a></div></div>"
			+ "<div><div class=\"avatar\"><a class=\"avtm\" target=\"_blank\" href=\"\">"
			+ "<img src=\"../userIcons/"
			+ topictop.userIcon
			+ "\">"
			+ "</a></div></div>"
			+ "<p>精华: "
			+ topictop.userBestTopicCount
			+ " </p><p>帖子: "
			+ topictop.userTopicCount
			+ " </p><p>积分:  "
			+ topictop.userPoint + "</p></div></td>");
	newTableNode.find("#maintr").append(newPerTDNode);
	// topic content
	newConTDNode = $("<td class=\"plc\"><div class=\"pi\">"
			+ "<strong><a id=\"postnum5728534\" onclick=\"setCopy(this.href, '帖子地址复制成功');return false;\" href=\"#\"> 顶楼</a></strong>"
			+ "<div class=\"pti\">"
			+ "<div class=\"pdbt\"> </div>"
			+ "<div class=\"authi\"> <img class=\"authicn vm\" id=\"authicon5728534\" src=\"../images/ico_lz.png\"> <em id=\"authorposton5728534\">发表于 <span title=\""
			+ topictop.CreatedOn
			+ "\">"
			+ topictop.CreatedOn
			+ "</span></em> </div>"
			+ "</div></div>"
			+ "<div class=\"pct\">"
			+ "<div class=\"pcb\">"
			+ -"<div class=\"t_fsz\">"
			+ topictop.Body
			+ "<div id=\"vfastpost\" class=\"fullvfastpost\">"
			+ "<table id=\"vfastposttb\" cellpadding=\"0\" cellspacing=\"0\">"
			+ "<tbody><tr>"
			+ "<td id=\"vf_l\"></td>"
			+ "<td id=\"vf_m\"><input style=\"color: rgb(205, 205, 205);\" name=\"message\" id=\"vmessage\" type=\"text\"/></td>"
			+ "<td id=\"vf_r\"></td>"
			+ "<td id=\"vf_b\"><button class=\"pn pnc\" name=\"replysubmit\" id=\"vreplysubmit\" value=\"true\" onclick=\"postReply('vmessage', '0', topic_id, user_id)\">post_newreply</button></td>"
			+ "</tr></tbody></table></div>"
			+ "<div id=\"vfastpostseccheck\"></div>"
			+ "<div id=\"comment_5728534\" class=\"cm\"> </div>"
			+ "<div id=\"post_rate_div_5728534\"></div></div></div></td>");
	newTableNode.find("#maintr").append(newConTDNode);
	// 操作控件 TODO 判断是否是楼主（可回复编辑） 是否是版主（可回复删除拉黑） 是否 是游客（可回复）
	newContrTDNode = $("<td class=\"plc\" style=\"overflow:visible;\"><div class=\"po hin\"><div class=\"pob cl\">"
			+ "<em><a class=\"fastre\" onclick=\"showEidtDialog(topic_id,'0')\" href=\"javascript:;\">回复</a>"
			+ "</em><p> </p></div></div></td>");
	newTableNode.find("#contr").append(newContrTDNode);

	return newTableNode;
}
// load 楼层
function setreplaylist(num, value) {
	var fnum = getfloor(num);
	var newTopicNode, newTableNode, newPerTDNode, newConTDNode, newContrTDNode;
	// TODO 顶楼节点ID
	newTopicNode = $("<div></div>").attr("id", "lc_" + num);
	newTableNode = $(
			"<table cellspacing=\"0\" cellpadding=\"0\" summary=\"pid5581684\"></table>")
			.addClass("plhin");
	newTableNode.append("<tbody></tbody>");
	newTableNode.children().append("<tr id=\"maintr\"></tr>");
	newTableNode.children().append("<tr></tr>");
	newTableNode.children().append(
			"<tr id=\"contr\"><td class=\"pls\"></td></tr>");
	newTableNode
			.children()
			.append(
					"<tr class=\"ad\"><td class=\"pls\"> </td><td class=\"plc\"> </td></tr>");
	// 用户个人信息 TODO ；用户个人信息链接 头像 帖子数量 积分 昵称 等级
	newPerTDNode = $("<td class=\"pls\" rowspan=\"2\"><div id=\"favatar5728534\" class=\"pls favatar\" style=\"top: 33px;\"><div class=\"pi\">"
			+ "<div class=\"authi\"><a class=\"xw1\" target=\"_blank\" href=\"\">"
			+ value.userNickName
			+ "</a></div></div>"
			+ "<div><div class=\"avatar\"><a class=\"avtm\" target=\"_blank\" href=\"\">"
			+ "<img src=\"../userIcons/"
			+ value.userIcon
			+ "\">"
			+ "</a></div></div>"
			+ "<p>精华: "
			+ value.userBestTopicCount
			+ " </p><p>帖子: "
			+ value.userTopicCount
			+ " </p><p>积分:  "
			+ value.userPoint + "</p></div></td>");
	newTableNode.find("#maintr").append(newPerTDNode);
	// topic content
	newConTDNode = $("<td class=\"plc\"><div class=\"pi\">"
			+ "<strong><a id=\"postnum5728534\" onclick=\"setCopy(this.href, '帖子地址复制成功');return false;\" href=\"#\">"
			+ fnum
			+ "楼</a></strong>"
			+ "<div class=\"pti\">"
			+ "<div class=\"pdbt\"> </div>"
			+ "<div class=\"authi\"> <img class=\"authicn vm\" id=\"authicon5728534\" src=\"../images/online_member.gif\"> <em id=\"authorposton5728534\">发表于 <span title=\""
			+ value.CreatedOn
			+ "\">"
			+ value.CreatedOn
			+ "</span></em> </div>"
			+ "</div></div>"
			+ "<div class=\"pct\">"
			+ "<div class=\"pcb\">"
			+ "<div class=\"t_fsz\">"
			+ value.Body
			+ "</div>"
			+ "<div id=\"comment_123123\" class=\"cm\"> <h3 class=\"psth xs1\"><span class=\"icon_ring vm\"></span>点评</h3></div>"
			+ "<div id=\"post_rate_div_5728534\"></div></div></div></td>");
	// load 评论
	$.each(value.replyMessage, function(num, mainValue) {
		setComment(newConTDNode.find("#comment_123123"), mainValue);
	});
	newTableNode.find("#maintr").append(newConTDNode);
	// 操作控件 TODO 判断是否是楼主（可回复编辑） 是否是版主（可回复删除拉黑） 是否 是游客（可回复）
	newContrTDNode = $("<td class=\"plc\" style=\"overflow:visible;\"><div class=\"po hin\"><div class=\"pob cl\">"
			+ "<span class=\"y\"><label for=\"manage5744487\"><input id=\"manage_"
			+ value.ID
			+ "\" name=\"topicInput\" class=\"pc\" type=\"checkbox\" autocomplete=\"off\" value=\""
			+ value.ID
			+ "\" onclick=\"showTools()\">管理</label></span>"
			+ "<em><a class=\"fastre\" onclick=\"showEidtDialog('"+value.ID+"','1')\" href=\"javascript:;\">回复</a>"
			+ "<a id=\"ajaxid_Sup_"
			+ value.ID
			+ "\" class=\"replyadd\" onclick=\"setSupPoint('"
			+ value.ID
			+ "','0',"
			+ value.SupPoint
			+ ")\" href=\"javascript:;\">支持&nbsp;</span>"
			+ "<a id=\"ajaxid_Aga_"
			+ value.ID
			+ "\" class=\"replysubtract\" onclick=\"setSupPoint('"
			+ value.ID
			+ "','1',"
			+ value.AgaPoint
			+ ")\" href=\"javascript:;\">反对&nbsp;</span>"
			+ "</em><p> </p></div></div></td>");
	newTableNode.find("#contr").append(newContrTDNode);

	return newTableNode;
}
// load 评论
function setComment(comDivNode, value) {
	var newCommentNode;
	newCommentNode = $("<div class=\"pstl xs1 cl\"></div>");
	newCommentNode
			.append("<div class=\"psta vm\"><a c=\"1\" href=\"\" mid=\"card_3997\"><img src=\"../userIcons/"
					+ value.userIcon
					+ "\"></a><a class=\"xi2 xw1\" href=\"\">&nbsp;"
					+ value.userNickName
					+ "</a></div>"
					+ "<div class=\"psti\">"
					+ value.Body
					+ "<span class=\"xg1\">发表于"
					+ "<span title=\""
					+ value.CreatedOn
					+ "\">"
					+ value.CreatedOn
					+ "</span>"
					+ "</span></div>");
	comDivNode.append(newCommentNode);
}
// 页管理
function setPagesTools(pageCount, pageNow) {
	var pageTools, pagelistNode;
	pagelistNode = $("<span></span>").attr("id", "fd_page_top");
	pagelistNode.append("<div class=\"pg\"></div>");
	for (var i = 1; i < pageCount + 1; i++) {
		if (i == pageNow) {
			pagelistNode.children().append("<strong>" + i + "</strong>");
		} else if (i == pageCount) {
			// 第i页点击事件
			pagelistNode.children()
					.append("<a class=\"last\">..." + i + "</a>");
		} else {
			// 第i页点击事件
			pagelistNode.children().append("<a>" + i + "</a>");
		}
	}
	pagelistNode
			.children()
			.append(
					"<label> <input class=\"px\" type=\"text\" onkeydown=\"if(event.keyCode==13) {window.location='forum.php?mod=forumdisplay&fid=865&page='+this.value;; doane(event);}\" value=\"1\" title=\"输入页码，按回车快速跳转\" size=\"2\" name=\"custompage\"> <span title=\"共 "
							+ pageCount
							+ "页\"> / "
							+ pageCount
							+ " 页</span> </label>");
	// TODO 下一页点击事件
	pagelistNode.children().append("<a class=\"nxt\" href=\"\">下一页</a>");

	pageTools = $("<div></div>").addClass("bm bw0 pgs cl").attr("id", "pgt");
	if (pageCount != "1"&&pageCount != "0") {
		pageTools.append(pagelistNode);
	}
	pageTools
			.append("<a id=\"post_replytmp\" title=\"回复\" href=\"javascript:;\" onclick=\"showEidtDialog(topic_id,'0')\"><img alt=\"回复\" src=\"../images/pn_reply.png\"></a>");
	return pageTools;
}
function setReplyEdite() {
	var content, userTdNode, editTdNode;
	content = $("<div></div>").addClass("pl bm bmw").attr("id", "f_pst");
	content
			.append("<table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr id='contr'></tr></tbody></table>");
	userTdNode = $("<td></td>").addClass("pls");
	userTdNode
			.append("<div class=\"avatar avtm\"><img src=\"../userIcons/XX.png\"></div>");
	content.find("#contr").append(userTdNode);

	editTdNode = $("<td></td>").addClass("plc");
	editTdNode
			.append("<div  class=\"cl\"><textarea id=\"editor\" name=\"editor\" rows=\"10\" style=\"width:99.4%;\"></textarea></div><p class=\"ptm pnpost\">"
					+ "<button id=\"fastpostsubmit\" class=\"pn pnc vm\" tabindex=\"5\" value=\"replysubmit\" name=\"replysubmit\" onclick=\"postReply('editor','0',topic_id,user_id)\" type=\"submit\"><strong>发表回复</strong></button></p>");
	content.find("#contr").append(editTdNode);
	return content;
}
function getfloor(i) {
	return ((pageNow - 1) * pageCount + i + 1);
}
